home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  13.8 KB  |  487 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    main.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revision Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include <stdlib.h>
  35.  
  36. #include "alloc.h"
  37. #include "apply.h"
  38. #include "array.h"
  39. #include "boolean.h"
  40. #include "bytestring.h"
  41. #include "character.h"
  42. #include "class.h"
  43. #include "deque.h"
  44. #include "dylan_lexer.h"
  45. #include "env.h"
  46. #include "error.h"
  47. #include "eval.h"
  48. #include "file.h"
  49. #include "function.h"
  50. #include "globals.h"
  51. #include "keyword.h"
  52. #include "list.h"
  53. #include "misc.h"
  54. #include "number.h"
  55. #include "object.h"
  56. #include "parse.h"
  57. #include "print.h"
  58. #include "read.h"
  59. #include "slot.h"
  60. #include "symbol.h"
  61. #include "syntax.h"
  62. #include "stream.h"
  63. #include "sys.h"
  64. #include "table.h"
  65. #include "values.h"
  66. #include "vector.h"
  67.  
  68.  
  69. void initialize_reservered_word_symbols (void);
  70. void initialize_marlais (void);
  71. extern Object binding_stack;
  72. extern FILE *yyin;
  73.  
  74. void yy_skip_ws (void);
  75. int charready (FILE *);
  76.  
  77. #ifdef MACOS
  78. int getopt (int argc, char *argv[], const char *options);
  79.  
  80. #endif
  81.  
  82. #ifndef INIT_FILE
  83. #define INIT_FILE "init.dyl"
  84. #endif
  85.  
  86. #ifndef VERSION
  87. #define VERSION "0.5.9"
  88. #endif
  89.  
  90. #ifdef MACOS
  91. #define main marlais_main
  92. int marlais_main (int argc, char *argv[]);
  93. extern void init_mac_prims (void);
  94. extern void yy_restart (FILE * fp);
  95.  
  96. // support for asynch loading of a file.
  97. Boolean theLoadFileFlag = false;
  98. char thePathToLoad[256];
  99. int thePromptDirty = false;
  100.  
  101. // support for general message handling.
  102. #include "MacMessages.h"
  103. #include "marlais_utils.h"
  104. #endif
  105.  
  106. static int do_not_load_init_file = 0;
  107. static char *optstring = "cdnep";
  108. static char *prompt = "? ";
  109. char *prompt_continuation = "> ";
  110. static int debug = 0;
  111. int echo_prefix = 0;
  112.  
  113. main (int argc, char *argv[])
  114. {
  115.     Object obj, eobj;
  116.     char *init_file;
  117.     int err, c;
  118.     extern char *optarg;
  119.     extern int optind;
  120.     struct frame *cache_env;
  121.  
  122.     int tok;
  123.  
  124.     /* banner */
  125.     printf ("Marlais %s\n", VERSION);
  126.  
  127.     /* initialization */
  128.     initialize_marlais ();
  129.     open_file_list = make_empty_list ();
  130.  
  131.     /* process command line parameters except source files */
  132.     while ((c = getopt (argc, argv, optstring)) != EOF) {
  133.     switch (c) {
  134.     case 'c':
  135.         classic_syntax = 1;
  136.         break;
  137.     case 'd':
  138.         debug = 1;
  139.         break;
  140.     case 'e':
  141.         echo_prefix = 1;
  142.         break;
  143.     case 'n':
  144.         do_not_load_init_file = 1;
  145.         break;
  146.     case 'p':
  147.         prompt_continuation = "";
  148.         break;
  149.     default:
  150.         fatal ("fatal: unrecognized option");
  151.     }
  152.     }
  153.  
  154.     /* error catch for initialization code */
  155.     err = setjmp (error_return);
  156.     if (err) {
  157.     printf ("; error in initialization code -- exiting.\n");
  158.     exit (1);
  159.     }
  160.     /* load initialization code */
  161.     if (!do_not_load_init_file) {
  162.     init_file = getenv ("MARLAIS_INIT");
  163.     if (!init_file) {
  164.         init_file = INIT_FILE;
  165.     }
  166. #ifdef INFIX_INIT_FILE
  167.     i_load (make_byte_string (init_file));
  168. #else
  169.     p_load (make_byte_string (init_file));
  170. #endif
  171.     }
  172.     set_module (new_module (dylan_user_symbol));
  173.     current_module ()->exported_bindings = all_symbol;
  174.  
  175.     use_module (dylan_symbol,
  176.         all_symbol,
  177.         make_empty_list (),
  178.         empty_string,
  179.         make_empty_list (),
  180.         all_symbol);
  181.  
  182.     /* load any source files specified on command line */
  183.     while (optind < argc) {
  184.     load (make_byte_string (argv[optind]));
  185.     optind++;
  186.     }
  187.  
  188.     load_file_context = 0;    /* <pcb> needs to be cleared after loading file */
  189.     cache_env = the_env;
  190.  
  191.     printf (prompt);
  192.     fflush (stdout);
  193.  
  194.     /* errors reset to here */
  195.     err = setjmp (error_return);
  196.     /* things to do on an error reset */
  197.     if (err) {
  198.     close_open_files ();
  199. #ifdef MACOS
  200.     fflush (stdout);
  201.  
  202.     /* so parser/scanner won't get confused. */
  203.     clearerr (stdin);
  204.     fflush (stdin);
  205.     yyrestart (stdin);
  206.  
  207.     /* is this a request to load a file? */
  208.     if (theLoadFileFlag) {
  209.         theLoadFileFlag = false;
  210.         load (make_byte_string (thePathToLoad));
  211.     }
  212.     /* some event caused the prompt to be dirty. */
  213.     if (thePromptDirty) {
  214.         printf (prompt);
  215.         fflush (stdout);
  216.         thePromptDirty = 0;
  217.     }
  218. #else
  219.     printf (prompt);
  220.     fflush (stdout);
  221.     yy_skip_ws ();
  222.     clearerr (stdin);
  223. #endif
  224.     if (trace_functions) {
  225.         printf ("; reset\n");
  226.         trace_level = 0;
  227.     }
  228.     load_file_context = 0;
  229.     the_env = cache_env;
  230.     eval_stack = 0;
  231.     push_eval_stack (current_module ()->sym);
  232.     num_debug_contexts = 0;
  233.     }
  234.     while ((obj = (classic_syntax ? read_object (stdin)
  235.            : parse_object (stdin, debug)))
  236.        && (obj != eof_object)) {
  237.     print_obj (eval (obj));
  238.     if (!classic_syntax) {
  239.         yy_skip_ws ();
  240.     }
  241.     if (!classic_syntax && charready (stdin)) {
  242.         continue;
  243.     }
  244.     if (classic_syntax < 0) {
  245.         classic_syntax = 0;
  246.     }
  247.     printf (prompt);
  248.     fflush (stdout);
  249.     cache_env = the_env;
  250.     }
  251.     return (0);
  252. }
  253.  
  254.  
  255. void
  256. initialize_marlais (void)
  257. {
  258.  
  259.     classic_syntax = 0;
  260.  
  261.     /* intialize garbage collector
  262.      */
  263.     initialize_gc ();
  264.  
  265.     dylan_symbol = make_symbol ("dylan");
  266.     dylan_user_symbol = make_symbol ("dylan-user");
  267.  
  268.     set_module (new_module (dylan_symbol));
  269.  
  270.     all_symbol = make_symbol ("all");
  271.     (current_module ())->exported_bindings = all_symbol;
  272.  
  273.     /* initialize symbol table primitives -- MUST BE DONE EARLY!! */
  274.     init_symbol_prims ();
  275.  
  276.     /* intialize global objects 
  277.      */
  278.     initialize_empty_list ();
  279.  
  280.     empty_string = make_byte_string ("");
  281.     equal_symbol = make_symbol ("=");
  282.  
  283.     true_object = make_true ();
  284.     false_object = make_false ();
  285.  
  286.     key_symbol = make_symbol ("#key");
  287.     keyword_symbol = make_symbol ("keyword");
  288.     required_symbol = make_symbol ("required");
  289.     allkeys_symbol = make_symbol ("#all-keys");
  290.     hash_rest_symbol = make_symbol ("#rest");
  291.     next_symbol = make_symbol ("#next");
  292.     values_symbol = make_symbol ("values");
  293.     hash_values_symbol = make_symbol ("#values");
  294.     quote_symbol = make_symbol ("quote");
  295.     eof_object = make_eof_object ();
  296.     unwind_symbol = make_symbol ("%unwind");
  297.     next_method_symbol = make_symbol ("next-method");
  298.     initialize_symbol = make_symbol ("initialize");
  299.     equal_hash_symbol = make_symbol ("=hash");
  300.     uninit_slot_object = make_uninit_slot ();
  301.     standard_input_stream = make_stream (Input, stdin);
  302.     standard_output_stream = make_stream (Output, stdout);
  303.     standard_error_stream = make_stream (Output, stderr);
  304.     quasiquote_symbol = make_symbol ("quasiquote");
  305.     unquote_symbol = make_symbol ("unquote");
  306.     unquote_splicing_symbol = make_symbol ("unquote-splicing");
  307.     element_symbol = make_symbol ("element");
  308.     element_setter_symbol = make_symbol ("element-setter");
  309.     signal_symbol = make_symbol ("signal");
  310.     concatenate_symbol = make_symbol ("concatenate");
  311.     cond_symbol = make_symbol ("cond");
  312.  
  313.  
  314.  
  315.  
  316.     /* often used keywords
  317.      */
  318.     getter_keyword = make_keyword ("getter:");
  319.     setter_keyword = make_keyword ("setter:");
  320.     until_keyword = make_keyword ("until:");
  321.     while_keyword = make_keyword ("while:");
  322.     else_keyword = make_keyword ("else:");
  323.     type_keyword = make_keyword ("type:");
  324.     deferred_type_keyword = make_keyword ("deferred-type:");
  325.     init_value_keyword = make_keyword ("init-value:");
  326.     init_function_keyword = make_keyword ("init-function:");
  327.     init_keyword_keyword = make_keyword ("init-keyword:");
  328.     required_init_keyword_keyword = make_keyword ("required-init-keyword:");
  329.     allocation_keyword = make_keyword ("allocation:");
  330.     super_classes_keyword = make_keyword ("superclasses:");
  331.     slots_keyword = make_keyword ("slots:");
  332.     debug_name_keyword = make_keyword ("debug-name:");
  333.     size_keyword = make_keyword ("size:");
  334.     fill_keyword = make_keyword ("fill:");
  335.     dim_keyword = make_keyword ("dimensions:");
  336.     min_keyword = make_keyword ("min:");
  337.     max_keyword = make_keyword ("max:");
  338.  
  339.     /* often used symbols
  340.      */
  341.     instance_symbol = make_symbol ("instance");
  342.     class_symbol = make_symbol ("class");
  343.     each_subclass_symbol = make_symbol ("each-subclass");
  344.     inherited_symbol = make_symbol ("inherited");
  345.     constant_symbol = make_symbol ("constant");
  346.     virtual_symbol = make_symbol ("virtual");
  347.     object_class_symbol = make_symbol ("object-class");
  348.  
  349.     obj_sym = make_symbol ("obj");
  350.     slot_val_sym = make_symbol ("slot-value");
  351.     set_slot_value_sym = make_symbol ("set-slot-value!");
  352.     val_sym = make_symbol ("val");
  353.     initial_state_sym = make_symbol ("initial-state");
  354.     next_state_sym = make_symbol ("next-state");
  355.     current_element_sym = make_symbol ("current-element");
  356.  
  357.     colon_equal_symbol = make_symbol (":=");
  358.     not_equal_symbol = make_symbol ("~=");
  359.     equal_equal_symbol = make_symbol ("==");
  360.     greater_equal_symbol = make_symbol (">=");
  361.     lesser_equal_symbol = make_symbol ("<=");
  362.     or_symbol = make_symbol ("|");
  363.     and_symbol = make_symbol ("&");
  364.     greater_symbol = make_symbol (">");
  365.     lesser_symbol = make_symbol ("<");
  366.     exponent_symbol = make_symbol ("^");
  367.     divides_symbol = make_symbol ("/");
  368.     times_symbol = make_symbol ("*");
  369.     minus_symbol = make_symbol ("-");
  370.     plus_symbol = make_symbol ("+");
  371.     not_symbol = make_symbol ("~");
  372.  
  373.     local_bind_symbol = make_symbol ("\"local-bind");
  374.     unbinding_begin_symbol = make_symbol ("\"unbinding-begin");
  375.     define_variable_symbol = make_symbol ("define-variable");
  376.     define_constant_symbol = make_symbol ("define-constant");
  377.     define_class_symbol = make_symbol ("define-class");
  378.     define_generic_function_symbol = make_symbol ("define-generic-function");
  379.     define_method_symbol = make_symbol ("define-method");
  380.     seal_symbol = make_symbol ("seal");
  381.     set_bang_symbol = make_symbol ("set!");
  382.     singleton_symbol = make_symbol ("singleton");
  383.     sealed_symbol = make_symbol ("sealed");
  384.     open_symbol = make_symbol ("open");
  385.     dynamism_keyword = make_keyword ("dynamism:");
  386.     negative_symbol = make_symbol ("negative");
  387.     list_symbol = make_symbol ("list");
  388.     pair_symbol = make_symbol ("pair");
  389.     variable_keyword = make_keyword ("variable:");
  390.     to_symbol = make_symbol ("to");
  391.     above_symbol = make_symbol ("above");
  392.     below_symbol = make_symbol ("below");
  393.     by_symbol = make_symbol ("by");
  394.     range_keyword = make_keyword ("range:");
  395.     collection_keyword = make_keyword ("collection:");
  396.     forward_iteration_protocol_symbol =
  397.     make_symbol ("forward-iteration-protocol");
  398.     plus_symbol = make_symbol ("+");
  399.     bind_symbol = make_symbol ("bind");
  400.     modifiers_keyword = make_keyword ("modifiers:");
  401.     abstract_symbol = make_symbol ("abstract");
  402.     concrete_symbol = make_symbol ("concrete");
  403.     primary_symbol = make_symbol ("primary");
  404.     free_symbol = make_symbol ("free");
  405.     use_symbol = make_symbol ("use");
  406.     export_symbol = make_symbol ("export");
  407.     create_symbol = make_symbol ("create");
  408.     module_symbol = make_symbol ("module");
  409.     define_module_symbol = make_symbol ("define-module");
  410.     module_keyword = make_keyword ("module:");
  411.     import_keyword = make_keyword ("import:");
  412.     exclude_keyword = make_keyword ("exclude:");
  413.     prefix_keyword = make_keyword ("prefix:");
  414.     rename_keyword = make_keyword ("rename:");
  415.     export_keyword = make_keyword ("export:");
  416.  
  417.  
  418.     /* infrequently used, but important symbols */
  419.     instance_slots_symbol = make_symbol ("%instance-slots");
  420.     class_slots_symbol = make_symbol ("%class-slots");
  421.     each_subclass_slots_symbol = make_symbol ("%each-subclass-slots");
  422.     x_symbol = make_symbol ("x");
  423.  
  424.     /* initialize table of syntax operators and functions
  425.      */
  426.     init_syntax_table ();
  427.     init_reserved_word_symbols ();
  428.     define_test_symbol = make_symbol ("define-test");
  429.     test_symbol = make_symbol ("test");
  430.     description_symbol = make_keyword ("description:");
  431.  
  432.     /* initialize builtin classes
  433.      */
  434.     init_class_hierarchy ();
  435.  
  436.     unspecified_object = make_unspecified_object ();
  437.  
  438.     /* make the unspecified object available
  439.      */
  440.     add_top_level_binding (make_symbol ("%unspecified"),
  441.                unspecified_object,
  442.                1);
  443.  
  444.     /* make the uninitialize slot value available
  445.      */
  446.     add_top_level_binding (make_symbol ("%uninitialized-slot-value"),
  447.                uninit_slot_object,
  448.                1);
  449.  
  450.     /* make default object
  451.      */
  452.     default_object = cons (false_object, false_object);
  453.     add_top_level_binding (make_symbol ("%default-object"), default_object, 1);
  454.  
  455.     binding_stack = cons (make_integer (0), make_empty_list ());
  456.  
  457.     /* initialize primitives
  458.      */
  459.     init_env_prims ();
  460.     init_list_prims ();
  461.     init_class_prims ();
  462.     init_slot_prims ();
  463.     init_file_prims ();
  464.     init_function_prims ();
  465.     init_values_prims ();
  466.     init_print_prims ();
  467.     init_number_prims ();
  468.     init_apply_prims ();
  469.     init_boolean_prims ();
  470.     init_keyword_prims ();
  471.     init_string_prims ();
  472.     init_vector_prims ();
  473.     init_error_prims ();
  474.     init_stream_prims ();
  475.     init_read_prims ();
  476.     init_table_prims ();
  477.     init_misc_prims ();
  478.     init_char_prims ();
  479.     init_deque_prims ();
  480.     init_array_prims ();
  481.     init_sys_prims ();
  482. #ifdef MACOS
  483.     init_mac_prims ();
  484. #endif
  485.  
  486. }
  487.